package workQueue;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import constant.Constants;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

public class ProducerDemo {

    public static void main(String[] args) throws IOException, TimeoutException {
        //建立连接
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(Constants.HOST);
        connectionFactory.setPort(Constants.PORT);
        connectionFactory.setUsername(Constants.USERNAME);
        connectionFactory.setPassword(Constants.PASSWORD);
        connectionFactory.setVirtualHost(Constants.VIRTUAL_HOST);
        Connection connection = connectionFactory.newConnection();

        //创建信道
        Channel channel = connection.createChannel();

        //声明交换机，使用内置交换机

        //声明队列
        channel.queueDeclare(Constants.QUEUE, true, false, false, null);

        //发送消息
        for (int i = 0; i < 10; i++) {
            String message = "hello" + i;
            channel.basicPublish("", Constants.QUEUE, null, message.getBytes(StandardCharsets.UTF_8));
        }

        //关闭连接
        channel.close();
        connection.close();

    }
}
